package org.bluedream.comm.utils;

import lombok.Data;

import java.io.Serializable;

/**
 * 返回数据
 *
 * @author mq
 */
@Data
public class RestResult<T> implements Serializable {
    //Suppress default constructor for noninstantiability
/*    private RestResult(){
        throw new AssertionError();
    }*/

    private static final long serialVersionUID = 1L;

    private static final int OK = 0;
    private static final String M_OK = "成功";
    private static final int ERROR = 500;
    private static final String M_ERROR = "未知异常，请联系管理员";

    /**
     * 错误代码
     */
    private int status;

    /**
     * 错误消息
     */
    private String msg;

    /**
     * 数据
     */
    private T data;

    public static RestResult<String> ok() {
        return ok(M_OK);
    }

    public static <T> RestResult<T> ok(T data) {
        RestResult<T> restResult = new RestResult<>();
        restResult.setMsg("");
        restResult.setStatus(OK);
        restResult.setData(data);
        return restResult;
    }

    public static RestResult<String> ok(String msg){
        RestResult<String> restResult = new RestResult<>();
        restResult.setMsg(msg);
        restResult.setStatus(OK);
        return restResult;
    }

    public static RestResult<String> error() {
        return error(M_ERROR);
    }

    public static RestResult<String> error(String msg) {
        return error(ERROR, msg);
    }

    public static RestResult<String> error(int code, String msg) {
        RestResult<String> restResult = new RestResult<>();
        restResult.setMsg(msg);
        restResult.setStatus(code);
        restResult.setData(null);
        return restResult;
    }

}
